#include "./server.hpp"

int main(){
    Socket sock;
    bool ret = sock.CreateServer(9001, "127.0.0.1", false);
    if(!ret) return -1;
    for(;;){
        int newfd = sock.Accept();
        if(newfd == -1) continue;
        Socket cli_sock(newfd);

        char buff[1024] = {0};
        int rcvlen = cli_sock.Recv(buff, sizeof(buff));
        DBG_LOG("recv: %d byte", rcvlen);
        if(rcvlen < 0){
            DBG_LOG("rcvlen < 0");
            cli_sock.Close();
            continue;
        }
        buff[rcvlen] = '\0';
        printf("server copied: %s", buff);
        cli_sock.Send(buff, rcvlen);
        DBG_LOG("echoed %s", buff);
        cli_sock.Close();
    }
    sock.Close();
    return 0;
}